import torch
from flask import jsonify
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from config.sleep_stages_config import  class_dict


def jsonToTensor(data):
    """将json数据转换为tensor"""
    # 动态获取数据长度，不再硬编码为3000
    data_array =  data.reshape(1, 1, -1)
    segment_tensor = torch.tensor(
        data_array, dtype=torch.float32)
    return segment_tensor

def result(predicted):
    '''根据预测结果返回对应的报告'''
    # 获取预测结果对应的数字编码
    report_code = class_dict.get(predicted, 'UNKNOWN')
    json_data = {"type": predicted, "report": str(report_code)}
    return jsonify(json_data)
